package cn.antho.shop.web.controller;

import cn.antho.shop.domain.Product;
import cn.antho.shop.feign.ProductService;
import cn.antho.shop.service.IShopBuyService;
import cn.antho.shop.domain.ShopBuy;
import cn.antho.shop.query.ShopBuyQuery;
import cn.antho.shop.result.JsonResult;
import cn.antho.shop.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shopBuy")
public class ShopBuyController {

    @Autowired
    public IShopBuyService shopBuyService;

    @Autowired
    private ProductService productService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody ShopBuy shopBuy){
        if(shopBuy.getId()!=null){
            shopBuyService.updateById(shopBuy);
        }else{
            shopBuyService.insert(shopBuy);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        shopBuyService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(shopBuyService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(shopBuyService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ShopBuyQuery query){
        Page<ShopBuy> page = new Page<ShopBuy>(query.getPage(),query.getRows());
        page = shopBuyService.selectPage(page);
        return JsonResult.success(new PageList<ShopBuy>(page.getTotal(),page.getRecords()));
    }

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public JsonResult add(@RequestBody ShopBuy shopBuy){
        shopBuyService.add(shopBuy);
        return JsonResult.success();
    }

    @RequestMapping(value="/accept/{id}",method=RequestMethod.GET)
    public JsonResult accept(@PathVariable("id")Long id){
        ShopBuy shopBuy = shopBuyService.getById(id);
        Product product = productService.getByProductId(shopBuy.getType());
        System.out.println(product);
        product.setStock(product.getStock() + shopBuy.getCount());
        productService.update(product);
        if (0 == shopBuy.getState()) {
            shopBuy.setState(1);
            shopBuyService.update(shopBuy, null);
        }
        return JsonResult.success();
    }

    @RequestMapping(value="/refuse/{id}",method=RequestMethod.GET)
    public JsonResult refuse(@PathVariable("id")Long id){
        ShopBuy shopBuy = shopBuyService.getById(id);
        if (0 == shopBuy.getState()) {
            shopBuy.setState(-1);
            shopBuyService.update(shopBuy, null);
        }
        return JsonResult.success();
    }


}
